/**
 * Created by tangwc on 2016/12/20
 */
var wechat = require('wechat-enterprise'),
    util = require('util'),
    async = $.async,
    redis = $.redis.createClient($.config.redis.server),
    api = new wechat.API($.config.enterprise.corpId, $.config.enterprise.corpsecret, $.config.agentid),

    httpUrl = $.config.httpUrl,
    socketUrl = $.config.socketUrl;
var array = [];
var peopleAtacks = [];
/*
 * 我要大礼包页面的  攻击并发两展示页面
 */
exports.getattack = function(req, res) {
    res.render('page/showattack', { httpUrl: httpUrl });
};
/*
 * 获取攻击数据
 */
exports.getdata = function(req, res) {
    var totalAttack = 0;
    //总的数据
    var data = {};
    redis.keys("users:*", function(err, uses) {
        async.each(uses, (user, rcallback) => {
            var name = user.split(":")[1];
            peopleAtacks.push({ name: name, count: 0 })
        }, function(err) {

        });

    });
    var stage_1 = [];
    var stage_2 = [];
    var stage_3 = [];
    var stage_4 = [];
    var stage_5 = [];
    var stage_6 = [];
    var stage_7 = [];
    var stage_8 = [];
    async.waterfall([

            function(cb) {

                redis.keys("tk:stage1:attacks:*", function(err, replies) {
                    async.each(replies, (tnum, rcallback) => {
                        var x = tnum.split(":")[3]
                        redis.llen(tnum, (err, length) => {
                            stage_1.push({ table: x, attacks: length });
                            totalAttack += length; //每轮的攻击次数增加到总次数里面
                            redis.lrange(tnum, 0, length, (err, result) => {
                                getSingle(result); //将每参与人员添加到临时数组中
                            })
                            rcallback();
                        });
                    }, function(err) {
                        cb();
                    });
                });
            },
            function(cb) {
                redis.keys("tk:stage2:attacks:*", function(err, replies) {

                    async.each(replies, (tnum, rcallback) => {
                        var x = tnum.split(":")[3]
                        redis.llen(tnum, (err, length) => {
                            stage_2.push({ table: x, attacks: length });
                            totalAttack += length; //每轮的攻击次数增加到总次数里面
                            redis.lrange(tnum, 0, length, (err, result) => {
                                getSingle(result); //将每参与人员添加到临时数组中
                            })
                            rcallback();
                        });
                    }, function(err) {
                        cb();
                    });
                });
            },
            function(cb) {
                redis.keys("tk:stage3:attacks:*", function(err, replies) {

                    async.each(replies, (tnum, rcallback) => {

                        var x = tnum.split(":")[3]
                        redis.llen(tnum, (err, length) => {
                            stage_3.push({ table: x, attacks: length });
                            totalAttack += length; //每轮的攻击次数增加到总次数里面
                            redis.lrange(tnum, 0, length, (err, result) => {
                                getSingle(result); //将每参与人员添加到临时数组中
                            })
                            rcallback();
                        });
                    }, function(err) {
                        cb();
                    });
                });
            },
            function(cb) {

                redis.keys("tk:stage4:attacks:*", function(err, replies) {

                    async.each(replies, (tnum, rcallback) => {

                        var x = tnum.split(":")[3]
                        redis.llen(tnum, (err, length) => {
                            stage_4.push({ table: x, attacks: length });
                            totalAttack += length; //每轮的攻击次数增加到总次数里面
                            redis.lrange(tnum, 0, length, (err, result) => {
                                getSingle(result); //将每参与人员添加到临时数组中
                            })
                            rcallback();
                        });
                    }, function(err) {
                        cb();
                    });
                });
            },
            function(cb) {
                redis.keys("tk:stage5:attacks:*", function(err, replies) {

                    async.each(replies, (tnum, rcallback) => {

                        var x = tnum.split(":")[3]
                        redis.llen(tnum, (err, length) => {
                            stage_5.push({ table: x, attacks: length });
                            totalAttack += length; //每轮的攻击次数增加到总次数里面
                            redis.lrange(tnum, 0, length, (err, result) => {
                                getSingle(result); //将每参与人员添加到临时数组中
                            })
                            rcallback();
                        });
                    }, function(err) {
                        cb();
                    });
                });
            },
            function(cb) {
                redis.keys("tk:stage6:attacks:*", function(err, replies) {

                    async.each(replies, (tnum, rcallback) => {

                        var x = tnum.split(":")[3]
                        redis.llen(tnum, (err, length) => {
                            stage_6.push({ table: x, attacks: length });
                            totalAttack += length; //每轮的攻击次数增加到总次数里面
                            redis.lrange(tnum, 0, length, (err, result) => {
                                getSingle(result); //将每参与人员添加到临时数组中
                            })
                            rcallback();
                        });
                    }, function(err) {
                        cb();
                    });
                });
            },
            function(cb) {
                redis.keys("tk:stage7:attacks:*", function(err, replies) {

                    async.each(replies, (tnum, rcallback) => {

                        var x = tnum.split(":")[3]
                        redis.llen(tnum, (err, length) => {
                            stage_7.push({ table: x, attacks: length });
                            totalAttack += length; //每轮的攻击次数增加到总次数里面
                            redis.lrange(tnum, 0, length, (err, result) => {
                                getSingle(result); //将每参与人员添加到临时数组中
                            })
                            rcallback();
                        });
                    }, function(err) {
                        cb();
                    });
                });
            },
            function(cb) {
                var tem = getTopPeople();
                async.each(tem, (attackUesr, rcallback) => {
                    redis.hgetall("users:{0}".format(attackUesr.name),(err,user)=>{
                        attackUesr.name = user.name;
                        rcallback();
                    })
                }, function(err) {
                    cb(null,tem);
                    // return tem;
                });
            }
        ],
        function(erro, data) {
            //非中奖、异常的退出
            if (erro) return res.send(erro);
            var _array = removespace();
            var count = _array.length;
            res.send({
                errCode: 0,
                data: { totalAttack: totalAttack, totalpoeple: count },
                toppeople: data,
                stage_1,
                stage_2,
                stage_3,
                stage_4,
                stage_5,
                stage_6,
                stage_7
            });
        });
};
Array.prototype.inArray = function(e) {
        for (i = 0; i < this.length; i++) {
            if (this[i] == e)
                return true;
        }
        return false;
    }
    //获取参与人员 并记录他们的求求数
function getSingle(arr) {
    for (var i = 0; i < arr.length; i++) {
        for (var j = 0; j < peopleAtacks.length; j++) {
            if (peopleAtacks[j].name == arr[i]) {
                peopleAtacks[j].count += 1;
            }
            if (!array.inArray(arr[i])) {
                array.push(arr[i]);
            }
        }
    }
}
//获取 发出请求数最高的是个人
function getTopPeople() {
    var tem = [];
    var arr = peopleAtacks;
    for (var i = 0; i < arr.length - 1; i++) {
        for (var j = 0; j < arr.length - 1 - i; j++) {
            if (arr[j].count < arr[j + 1].count) {
                var t = arr[j];
                arr[j] = arr[j + 1];
                arr[j + 1] = t;
            }
        }
    }
    for (var j = 0; j < 10; j++) {
        tem.push(arr[j]);
    }
    peopleAtacks = [];
    return tem;
}

function removespace() {
    for (var i = 0; i < array.length; i++) {
        if (array[i] == "" || typeof(array[i]) == "undefined") {
            array.splice(i, 1);
            i = i - 1;
        }
    }
    return array;
}